<?php

namespace app\api\controller;

use app\admin\model\UserBase;
use app\BaseController;
use app\common\traits\JumpTrait;
use app\Request;
use EasyWeChat\Factory;
use think\App;

/**
 * Class Api H5接口统一底层控制器
 * @package app\api\controller
 * Author FashionJune
 */
class Api extends BaseController
{

    protected string $client;//客户端类型
    protected object $wechat;//微信公众号快捷使用类
    protected object $alipay;//支付宝公众号快捷使用类
    public object $redis;//redis服务类
    protected int $page;
    protected int $per;
    protected  $user;//缓存用户信息
    protected string $PayTypCd;//金燕e付付款方式（B支付宝扫码支付、C微信扫码支付、D银联扫码支付）接口变量名就这样，不是我少写了e

    use JumpTrait;

    public function __construct(App $app, Request $request)
    {
        parent::__construct($app);
        $this->client = session('client_type') ?? '';
//        session('wechat_user', null);
//        dd(session('wechat_user'));
//        dd($request->user);
        if ($this->client == 'wechat') {
            $this->PayTypCd = 'C';
            $wechat_config = [
                'app_id' => env('WECHAT.APP_ID'),
                'secret' => env('WECHAT.SECRET'),
//                'aes_key' => env('WECHAT.AES_KEY'),
            ];
            $this->wechat = Factory::officialAccount($wechat_config);
            //获取jsjdk
            $functions = ['getLocation', 'openLocation'];
//            dd(url('',[],'.html',true)->build(), $this->request->url());
            $this->wechat->jssdk->setUrl($this->request->url(true));
            $wechat_config = $this->wechat->jssdk->buildConfig($functions, false);
            $this->assign('wechat_config', json_encode($wechat_config));
//            $this->user = session('wechat_user');
        } elseif ($this->client == 'alipay') {//支付宝静默获取，有可能新注册用户session更新不及时
            $this->PayTypCd = 'B';
//            $this->user = session('alipay_user');
        } else {

        }
        $this->user = $request->user;
//        dd($this->user);
        //初始化分页信息
        $this->page = (int)$this->request->param('page', 1);
        $this->per = (int)$this->request->param('per', 10);
        $this->redis = new \Redis();
        $this->redis->connect('127.0.0.1', 6379);
        $auth = $this->redis->auth('dpguo'); //设置密码
        //页面通用变量
        $this->assign('controller', $this->request->controller());
        $this->assign('action', $this->request->action());
        $this->assign('client_type', $this->client);
    }


    /**
     * 模板变量赋值
     * @param string|array $name 模板变量
     * @param mixed $value 变量值
     * @return mixed
     */
    public function assign($name, $value = null)
    {
        return $this->app->view->assign($name, $value);
    }

    /**
     * 解析和获取模板内容 用于输出
     * @param string $template
     * @param array $vars
     * @return mixed
     */
    public function fetch($template = '', $vars = [])
    {
        return $this->app->view->fetch($template, $vars);
    }


    /**
     * @title 接口状态
     * @author FashionJune
     */
    public function websiteStatus()
    {
        $data = $this->request->except(['app_id']);
        api_success($data, '接口正常');
    }


}
